package com.jumei.flume.common;

/**
 * Created by ZhongGang
 * at 2017/7/5 16:03
 */
public class StringUtils {
    // 空字符串
    public static final String EMPTY_STRING = "";
    public static final char UNDERLINE = '_';

    /**
     * trim之后，判断是否为空串.
     *
     * @param value 验证值
     * @return 若为null，则返回false
     */
    public static boolean isEmptyStringTrim(String value) {
        if (null == value) {
            return true;
        }

        return StringUtils.EMPTY_STRING.equals(value.trim());
    }

    public static String camelToUnderline(String param) {
        if (param == null || "".equals(param.trim())) {
            return "";
        }
        int len = param.length();
        StringBuilder sb = new StringBuilder(len);
        for (int i = 0; i < len; i++) {
            char c = param.charAt(i);
            if (Character.isUpperCase(c)) {
                sb.append(UNDERLINE);
                sb.append(Character.toLowerCase(c));
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }

    public static String underlineToCamel(String param){
        if (param==null||"".equals(param.trim())){
            return "";
        }
        int len=param.length();
        StringBuilder sb=new StringBuilder(len);
        for (int i = 0; i < len; i++) {
            char c=param.charAt(i);
            if (c==UNDERLINE){
                if (++i<len){
                    sb.append(Character.toUpperCase(param.charAt(i)));
                }
            }else{
                sb.append(c);
            }
        }
        return sb.toString();
    }
    
//    public static String safeString(String p, String defaultString) {
//    	
//    	return null == p ? defaultString : p;
//    }
    
    public static <T> T safe4Param(T p, T defaultString) {
    	
    	return null == p ? defaultString : p;
    }
}
